package com.library.books.service.impl;

import com.library.books.mapper.SysCountMapper;
import com.library.books.service.ISysCountService;
import com.library.system.domain.dto.*;
import com.library.system.domain.vo.datapanel.SysDataPanelLendBookCountVo;
import com.library.system.domain.vo.datapanel.SysDataPanelOverdueRecordBookCountVo;
import com.library.system.domain.vo.datapanel.SysDataPanelReturnBookCountVo;
import com.library.system.domain.vo.datapanel.SysDataPanelVo;
import com.library.system.domain.vo.home.SysHomeLendCountVo;
import com.library.system.domain.vo.home.SysHomeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @Package: com.library.books.service.impl
 * @ClassName: SysHomeService
 * @Author: Yin
 * @Date: 11:13
 * @Description: 此文件描述信息
 */
@Service
public class SysCountServiceImpl implements ISysCountService {

    @Autowired
    SysCountMapper countMapper;

    /**
     * 系统首页信息
     * @param dto 请求参数
     * @return home vo
     */
    @Override
    public SysHomeVo home(HomeDto dto) {
        SysHomeVo sysHomeVo = new SysHomeVo();

        sysHomeVo.setCurBookNum(countMapper.selectCurrentLifeBooksNumber());
        sysHomeVo.setCurLendNum(countMapper.selectCurrentLifeLendBooksNumber());
        sysHomeVo.setLendStudentNum(countMapper.selectStudentTotalNumber());
        sysHomeVo.setLendBooksNum(countMapper.selectBorrowBooksNumber());

        // 查询月份借阅统计
        List<SysHomeLendCountVo> sysHomeLendCountVos = countMapper.selectLendBooksCount(dto.getLendCount());
        if(sysHomeLendCountVos.size() != 12) {
            // 补齐十二个月
            Long[] month = sysHomeLendCountVos.stream().map(SysHomeLendCountVo::getMonth).toArray(Long[]::new);
            Arrays.sort(month);
            for (long i = 1 ; i <= 12; i++) {
                if(Arrays.binarySearch(month,i) < 0) {
                    sysHomeLendCountVos.add(SysHomeLendCountVo.emptyMonth(i));
                }
            }
            sysHomeLendCountVos.sort(Comparator.comparing(SysHomeLendCountVo::getMonth));
        }
        sysHomeVo.setLendMonthCount(sysHomeLendCountVos);
        // 查询编目统计
        sysHomeVo.setTypeCount(countMapper.selectBooksTypeCount());
        return sysHomeVo;
    }

    /**
     * 数据面板数据
     * @param dto
     * @return
     */
    @Override
    public SysDataPanelVo dataPanel(DataPanelDto dto) {
        SysDataPanelVo dataPanel = new SysDataPanelVo();

        // 借阅统计
        List<SysDataPanelLendBookCountVo> SysDataPanelLendBookCountVos = countMapper.selectDataPanelLendBooksCount(dto.getLend());
        dataPanel.setLendCount(SysDataPanelLendBookCountVos);

        // 归还统计
        List<SysDataPanelReturnBookCountVo> SysDataPanelReturnBookCountVos = countMapper.selectDataPanelReturnBooksCount(dto.getReturns());
        dataPanel.setReturnCount(SysDataPanelReturnBookCountVos);

        // 逾期记录统计
        List<SysDataPanelOverdueRecordBookCountVo> SysDataPanelOverdueRecordBookCountVos = countMapper.selectDataPanelOverdueBooksRecordCount(dto.getOverdue());
        dataPanel.setOverdueCount(SysDataPanelOverdueRecordBookCountVos);

        return dataPanel;
    }

    /**
     * 查询数据面板 书籍借阅统计
     */
    @Override
    public List<SysDataPanelLendBookCountVo> selectDataPanelLendBooksCount(SysDataPanelLendBookCountDto dto) {
        return countMapper.selectDataPanelLendBooksCount(dto);
    }

    /**
     * 查询数据面板 书籍归还统计
     */
    @Override
    public List<SysDataPanelReturnBookCountVo> selectDataPanelReturnBooksCount(SysDataPanelReturnBookCountDto dto) {
        return countMapper.selectDataPanelReturnBooksCount(dto);
    }

    /**
     * 查询数据面板 书籍逾期记录统计
     */
    @Override
    public List<SysDataPanelOverdueRecordBookCountVo> selectDataPanelOverdueBooksRecordCount(SysDataPanelOverdueBookRecordCountDto dto) {
        return countMapper.selectDataPanelOverdueBooksRecordCount(dto);
    }
}
